<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <template #toolbar>
        <!-- <a-button type="default" @click="handleWlImport()">
          <Icon icon="i-ant-design:upload-outlined" /> {{ t('物流信息导入') }}
        </a-button>
        <a-button type="primary" danger ghost @click="handleWlForm()">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增物流信息') }}
        </a-button> -->
        <a-button type="default" @click="handleImport()">
          <Icon icon="i-ant-design:upload-outlined" /> {{ t('导入') }}
        </a-button>
        <a-button type="default" @click="handleExport()">
          <Icon icon="i-ant-design:download-outlined" /> {{ t('导出') }}
        </a-button>
        <Popconfirm placement="bottom" :title="t('是否确认批量审核？')" @confirm="handleConfirm">
          <a-button type="default" v-auth="'wh:rd02:rd02:edit'">
            <Icon icon="ant-design:check-outlined" /> {{ t('批量审核') }}
          </a-button>
        </Popconfirm>
        
        <a-button type="error" ghost @click="handleForm({ bred: '0' })" v-auth="'wh:rd02:rd02:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增红字') }}
        </a-button>
        <a-button type="success" ghost @click="handleForm({ bred: '1' })" v-auth="'wh:rd02:rd02:edit'">
          <Icon icon="i-fluent:add-12-filled" /> {{ t('新增蓝字') }}
        </a-button>
      </template>
      <template #firstColumn="{ record }">
        <a @click="handleForm({ code: record.code })">
          {{ record.code }}
        </a>
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <InputWlForm @register="registerWlDrawer" @success="handleSuccess" />
    <BpmRuntimeTrace @register="registerTraceModal" />
    <FormImport @register="registerImportModal" @success="handleSuccess" />
    <FormWlImport @register="registerWlImportModal" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup name="ViewsWmsWhRd02Rd02List">
  import { unref, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { useGlobSetting } from '/@/hooks/setting';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { downloadByUrl } from '/@/utils/file/download';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { rd02Delete, rd02ListData, batchAudit } from '/@/api/wms/wh/rd02/rd02';
  import { useDrawer } from '/@/components/Drawer';
  import { useModal } from '/@/components/Modal';
  import { BpmRuntimeTrace } from '/@/components/Bpm';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  import InputWlForm from './formWl.vue';
  import FormImport from './formImport.vue';
  import FormWlImport from './formWlImport.vue';
  import { Popconfirm } from 'ant-design-vue';
  import { useUserStore } from '/@/store/modules/user';
  import { getSearchFormSchemas, getTableColumns, getActionColumn, fieldMapToTime } from './config';
  const userStore = useUserStore();

  const { t } = useI18n('wh.rd02.rd02');
  const { showMessage } = useMessage();
  const { meta } = unref(router.currentRoute);

  const getTitle = {
    icon: meta.icon || 'i-ant-design:book-outlined',
    value: meta.title || t('出库主表管理'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 90,
    schemas: getSearchFormSchemas(t, handleSuccess),
    fieldMapToTime,
  };

  const tableColumns: BasicColumn[] = getTableColumns(t);

  const actionColumn: BasicColumn = getActionColumn(t, handleForm, handleDelete, handleTrace);

  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerWlDrawer, { openDrawer: wlOpenDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows, setProps }] = useTable({
    api: rd02ListData,
    beforeFetch: (params) => {
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  function handleWlForm(record: Recordable) {
    if (getSelectRows().length) {
      let selIds = getSelectRows()[0].id
      wlOpenDrawer(true, selIds);
    }else{
      showMessage('请先选择数据');
    }
  }

  async function handleExport() {
    const { ctxAdminPath } = useGlobSetting();
    downloadByUrl({
      url: ctxAdminPath + '/wh/rd02/rd02/exportData',
      params: getForm().getFieldsValue(),
    });
  }

  const [registerImportModal, { openModal: importModal }] = useModal();
  const [registerWlImportModal, { openModal: wlImportModal }] = useModal();

  function handleImport() {
    importModal(true, {});
  }

  async function handleDelete(record: Recordable) {
    const params = { code: record.code };
    const res = await rd02Delete(params);
    showMessage(res.message);
    handleSuccess(record);
  }

  async function handleConfirm() {
    if (getSelectRows().length) {
      let selIds =  getSelectRows().map((item) => item.id).join(',')
      const res = await batchAudit({ selIds });
      showMessage(res.message);
      handleSuccess({});
    } else {
      showMessage('请先选择数据');
    }
  }

  function handleSuccess(record: Recordable) {
    reload({ record });
  }

  const [registerTraceModal, { openModal: traceModel }] = useModal();

  function handleTrace(record: Recordable) {
    traceModel(true, { formKey: 'flow_wh_rd02', bizKey: record.id });
  }

  onMounted(async () => {
    setProps()
    handleCompany();
  });

  async function handleCompany() {
    await getForm().setFieldsValue({
      companyCode: userStore.getProjecte.code,
      status: '',
      bsend:'0'
    });
    reload();
    await getForm().setFieldsValue({
      companyCode: userStore.getProjecte.code,
      status: '',
      bsend:'0'
    });
  }

  function handleWlImport() {
    wlImportModal(true, {});
  }
</script>
